<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      label-width="140px"
      :model="formData"
      :rules="formRules"
      ref="form"
    >
      <el-form-item label="分类名称" prop="catename" >
        <el-input v-model="formData.catename" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="图标class" prop="icon">
        <el-input v-model="formData.icon" placeholder="请输入" 
        ></el-input>
      </el-form-item>
      <el-form-item label="排序大小" prop="sort_num">
         <el-input-number v-model="formData.sort_num" @change="handleChange" :min="1" :max="100" label="描述文字"></el-input-number>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="onSubmit"
          >提交</el-button
        >
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addAdvCateInfoApi, updateAdvCateInfoApi } from "@/api/adv_cate";
export default {
  name: "addFormCate",
  data() {
    return {
      //弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        catename: "",
        icon: "",
        sort_num:0,
      },
      // 表单验证规则
      formRules: {
         catename: [{ required: true, message: "请输入", trigger: "blur" }],
        icon: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      // 计数器
      num: 1
    };
  },
  computed: {
    title() {
      return this.formData.id ? "修改" : "添加";
    },
  },
  methods: {
    // 计数器
     handleChange(value) {
        console.log(value);
      },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate();
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateAdvCateInfoApi(this.formData)
        : await addAdvCateInfoApi(this.formData);
      // 提示效果
      this.$message.success(this.formData.id ? "修改成功" : "添加成功");
      // 关闭弹框
      this.dialogShow = false;
      // 列表更新
      this.$emit("updateList");
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        catename: "",
        icon: "",
        sort_num: "",
      };
      // 移除校验
      this.$refs.form.clearValidate();
    },
  },
};
</script>
<style scoped></style>
